import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class Program {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		if(args.length == 0)
			WriteToXml("agileProject", "C:\\temp\\agileProject.xml");
		else if(args[0].equals("-load"))
			ReadFromXml("c:\\temp\\agileProject.xml");		
			
	}
	
	private static void WriteToXml(String projectName, String xmlFileName)
	{
		Project aProject = new Project(projectName, xmlFileName, 1);
		
		XStream xStream = new XStream(new DomDriver());
		xStream.alias("project", Project.class);
		
		String xmlFile = xStream.toXML(aProject);
		
		Write(xmlFile, xmlFileName);
	}
	
	private static void ReadFromXml(String xmlFileName)
	{
		Project aProject = null;
		XStream xStream = new XStream(new DomDriver());
		try 
		{
			xStream.alias("project", Project.class);
			
			aProject = (Project)xStream.fromXML(new FileInputStream(xmlFileName));			
			
			System.out.println("Project has been loaded into memory:");
			System.out.println("------------------------------------");
			System.out.println("Project Name: " + aProject.getProjectName());
			System.out.println("Build Number: " + aProject.getBuildNumber());
			System.out.println("Project Path: " + aProject.getBuildPath());
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void Write(String xmlFile, String xmlFileName) 
	{
		PrintWriter fileWriter = null;
		
		try 
		{
			fileWriter = new PrintWriter(new FileOutputStream(xmlFileName));
			fileWriter.write(xmlFile);
			fileWriter.close();
		} 
		catch (FileNotFoundException e) 
		{			
			e.printStackTrace();
		}
		finally
		{
			if(fileWriter != null) fileWriter.close(); 
		}
		
	}

}
